<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" errorPage="/error.jsp"%>
<%@page import="dbexp.model.Sequence"%>
<%@page import="dbexp.model.db.DbUser"%>
<%@page import="dbexp.connection.DbMetaData"%>
<%
DbUser u = (DbUser)session.getAttribute("USER");
String name = request.getParameter("name");
String schema = request.getParameter("schema");
boolean nowy = (name==null || name.trim().equals("null")) && (schema==null || schema.trim().equals("null"));
String id = schema+"-"+name;
String action="update";
String initial = "Ostatnia wartość";
String readOnly= ",readOnly:true";
Sequence ob  = null;
String min = "1";
String max = "1";
String inc = "1";
int last = 1;
if(nowy){
	action="create";
	schema=u.getSchemaName();
	name="Nowa sekwencja";
	initial = "Początkowa wartość";
	readOnly="";
}
String save = "Zapisz";
if(nowy){
	save = "Utwórz";
}else{
	ob = DbMetaData.getSequence(u, name, schema);
	min = ob.getMin();
	max = ob.getMaxValue();
	inc = ob.getIncrementBy();
	last = ob.getLastNumber();
}
%>
<div class="container" style="width:500px">
    <div id="<%=id%>-form"></div>
</div>
<script language="JavaScript">

SequenceForm = Ext.extend(Ext.form.FormPanel, {
    renderTo: '<%=id%>-form',
    iconCls: 'db-function-icon',
    frame: true,
    labelAlign: 'left',
    title: '<%=id%>',
    width: 850,
    url:'db/sequence.jsp',
    initComponent : function() {
   		 var config={
        	defaultType: 'textfield',
        	defaults: {
            	anchor: '100%'
       		},
       		monitorValid:true,
       	    items: [
       	    	{fieldLabel: 'Nazwa', name: 'name', allowBlank: false,value:'<%=name %>'<%=readOnly %>},
       	        <%=(nowy)?"combo({emptyText:'"+schema+"'}),":"{fieldLabel:'Schema',name:'schema',allowBlank:false,value:'"+schema+"',readOnly:true},"%>
       	     {
                 xtype:'fieldset',
					title: 'Wartości',
					autoHeight:true,
					items:[{
						layout:'column',
						defaults : {
						bodyStyle : 'padding:10px',
                       layout:"form"
                     },
						items:[{
							bodyStyle : 'padding:10px',
							layout:"form",
							items:[{
								xtype:'numberfield',
								fieldLabel:'Minimum',
								name:'min',
								value:<%=min%>},{
								xtype:'numberfield',
								fieldLabel:'Maksimum',
								name:'max',
								value:<%=max%>}]
						},{
							layout:"form",
							items:[{
								xtype:'numberfield',
								fieldLabel:'Zwiększana o',
								name:'inc',
								value:<%=inc%>},{
								xtype:'numberfield',
								fieldLabel:'${initial}',
								name:'last',
								value:<%=last%><%=readOnly %>}]
						}]
					}]
             }
       	    ],
       	    buttons:[{
       	    	text: 'Zapisz',
       	    	iconCls: 'icon-save',
       	    	formBind:true,
       	    	scope: this,
       	    	handler:this.submit
           	},{
           		text: 'Odśwież',
           		scope: this,
                handler: function(btn, ev){
                	this.getForm().load({url:'db/sequence.jsp',params:{name:'${name}',schema:'${schema}',action:'get'},method:'POST'});
            	}
            }]
       };

   		Ext.apply(this, Ext.apply(this.initialConfig, config));
        SequenceForm.superclass.initComponent.apply(this, arguments);
    },
	onRender:function() {
	    SequenceForm.superclass.onRender.apply(this, arguments);
	    this.getForm().waitMsgTarget = this.getEl();
    },
	onLoadClick:function() {
    	this.load({
	    	url:this.url
	    	,waitMsg:'Ładowanie...'
	    	 ,params:{action:'get'}
    	});
    },
    submit:function() {
    	this.getForm().submit({
    		url:this.url,
    		scope:this,
    		success:this.onSuccess,
    		failure:this.onFailure,
    		params:{action:'<%=action%>'},
    		waitMsg:'Trwa zapisywanie danych...'
    	});
   	},
   	onSuccess:function(form, action) {
   		obj = Ext.util.JSON.decode(action.response.responseText);
        var root = Ext.getCmp('api-tree').getRootNode();
    	if( (catNode=root.findChild("t","609"))!=null){
    		catNode.reload();
    	}
   		<%if(nowy){%>
    	Ext.Msg.alert('DbExplorer','Z powodzeniem utworzono nową sekwencję');
		var w = Ext.getCmp('create-window');
		if(w){
			w.close();
		}
		<%}%>
    },
    onFailure:function(form, action) {
    		this.showError(action.response.responseText);
    },
    showError:function(msg) {
    	obj = Ext.util.JSON.decode(msg);
    	 title = obj.error.title||'Error';
    	 Ext.Msg.show({
    		title:title,
    	 	msg:obj.error.msg||obj.error,
    	 	modal:true,
    	 	icon:Ext.Msg.ERROR,
    	 	buttons:Ext.Msg.OK
    	 });
    }
});
Ext.onReady(function() {
	Ext.form.Field.prototype.msgTarget = 'side';
    var functionForm = new SequenceForm({
        renderTo: '<%=id%>-form'
    });
   // functionForm.load({url:'db/sequence.jsp',params:{action:'get',name:'${name}',schema:'${schema}'},method:'POST'});
});
</script>